package pictureRater;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.util.Log;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class Data {
	private static AmazonS3Client s3=null;
	private static ObjectListing objList=null;
	//validating credentials to server
	public static AmazonS3 getInstance(){
		return server();
	}
	//getting bucket names
	public static List<String> getBuckets(){
		List buckets=getInstance().listBuckets();
		List<String> bucketNames=new ArrayList<String>(buckets.size());
	Iterator<Bucket>bucketIterator=buckets.iterator();
	while(bucketIterator.hasNext()){
		bucketNames.add(bucketIterator.next().getName());
	}
	return bucketNames;
	}
	//getting object names from bucket
	public static List<String> getBucketObjectNames(String b_name){
		ObjectListing objects=getInstance().listObjects(b_name);
		objList=objects;
		List<String> objNames=new ArrayList<String>(objects.getObjectSummaries().size());
		Iterator<S3ObjectSummary> objIterator=objects.getObjectSummaries().iterator();
	while(objIterator.hasNext()){
		objNames.add(objIterator.next().getKey());
	}
	return objNames;
	}
	//getting object names from bucket
	public static List<String> getObjectNamesForBucket( String b_name , int item_number) {
		ListObjectsRequest request= new ListObjectsRequest();
		request.setMaxKeys(new Integer(item_number));
		request.setBucketName(b_name);
		ObjectListing objects = getInstance().listObjects( request );
		objList = objects;
		List<String> objectNames = new ArrayList<String>( objects.getObjectSummaries().size());
		Iterator<S3ObjectSummary> objIterator = objects.getObjectSummaries().iterator();
		while(objIterator.hasNext()){
			objectNames.add(objIterator.next().getKey());
		}

		return objectNames;		
	}
	//updating list of names from bucket
	public static List<String> getMoreObjectNamesForBucket() {
		try{
			ObjectListing objects = getInstance().listNextBatchOfObjects(objList);
			objList = objects;
			List<String> objectNames = new ArrayList<String>( objects.getObjectSummaries().size());
			Iterator<S3ObjectSummary> objIterator = objects.getObjectSummaries().iterator();
			while(objIterator.hasNext()){
				objectNames.add(objIterator.next().getKey());
			}
			return objectNames;
		} catch (NullPointerException e){
			return new ArrayList<String>();
		}

	}
	
	//adding object to bucket
	public static void createObjectForBucket( String b_name, String obj_name, String data ) {
		try {
			ByteArrayInputStream byte_stream = new ByteArrayInputStream( data.getBytes() );
			ObjectMetadata metadata = new ObjectMetadata();
			metadata.setContentLength( data.getBytes().length );
			getInstance().putObject(b_name, obj_name, byte_stream, metadata );
		}
		catch ( Exception exception ) {
			Log.e( "TODO", "createObjectForBucket" );
		}
	}
	//removing object from bucket
	public static void deleteObject( String b_name, String obj_name ) {
		getInstance().deleteObject( b_name, obj_name );
	}
	//reading object
	public static String getDataForObject( String b_name, String obj_name ) {
		return read( getInstance().getObject( b_name, obj_name ).getObjectContent() );
	}
	//stream for reading data
	protected static String read( InputStream stream ) {
		try {
			ByteArrayOutputStream byte_out = new ByteArrayOutputStream( 8196 );
			byte[] buffer = new byte[1024];
			int length = 0;
			while ( ( length = stream.read( buffer ) ) > 0 ) {
				byte_out.write( buffer, 0, length );
			}
			
			return byte_out.toString();
		}
		catch ( Exception exception ) {
			return exception.getMessage();
		}
	}
	public static AmazonS3Client server(){
		validateCredentials();
		return s3;
	}

	private static void validateCredentials() {
	    if ( s3 == null ) {            	

            AWSCredentials credentials = new BasicAWSCredentials( properties.getInstance().getAccessKey(), properties.getInstance().getSecretKey() );
		    s3 = new AmazonS3Client( credentials );
	    }
	}
	public void clear(){
		s3=null;
	}
}
